package leetcode.lt100;

class Solution_65 {
	public boolean isNumber(String s) {

		s = s.trim();
		char[] c = s.toCharArray();
		int[][] FSM = { 
				{ -1,  0,  1, -1, 2 }, 
				{ -1, -1, -1, -1, 3 }, 
				{ -1, -1,  4,  5, 2 }, 
				{ -1, -1, -1,  5, 3 },
				{ -1, -1, -1,  5, 4 }, 
				{ -1,  5, -1, -1, 6 }, 
				{ -1, -1, -1, -1, 6 } };

		int now = 0;
		for (int i = 0; i < c.length; i++) {
			switch (c[i]) {
			case '-':
				now = FSM[now][1];
				break;
			case '+':
				now = FSM[now][1];
				break;
			case '.':
				now = FSM[now][2];
				break;
			case 'e':
				now = FSM[now][3];
				break;
			case 'E':
				now = FSM[now][3];
				break;
			default: {
				if (c[i] >= '0' && c[i] <= '9')
					now = FSM[now][4];
				else
					now = FSM[now][0];
			}
			}
			if (now == -1)
				return false;
		}
		return now == 2 || now == 3 || now == 4 || now == 6;
	}

}